package com.catchegg.springcloud.controller;

import com.catchegg.springcloud.controller.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author noreply
 * @description
 * @datetime 2021/11/3 22:20
 */
@RestController
@RequestMapping("/consumer")
@DefaultProperties(defaultFallback = "globalFallBack")
public class OrderController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/info/ok/{id}")
    @HystrixCommand
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        int a = 1/id;
        String res = paymentHystrixService.paymentInfo_OK(id);
        System.out.println(res);
        return res + (1/id);
    }

    ;

    @GetMapping("/info/error/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_ERROR_Handler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
    })
    public String paymentInfo_ERROR(@PathVariable("id") Integer id) {
        return paymentHystrixService.paymentInfo_ERROR(id);
    }

    public String paymentInfo_ERROR_Handler(@PathVariable("id") Integer id) {
        return "80____paymentInfo_ERROR_Handler___异常返回";
    }

    public String globalFallBack() {
        return "80-defaultFallback id = ";
    }
}
